<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="70px"
    >
      <el-form-item label="名称" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择名称">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.typeName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消费金额" prop="mainMoney">
        <el-input
          v-model="queryParams.mainMoney"
          placeholder="请输入消费金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:main:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:main:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:main:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:main:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20" class="mb8" v-hasPermi="['system:main:edit']">
      <el-col :span="1.5">
        <el-tag type="danger">总支出：{{ total_expenditure }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="success">总收入：{{ total_income }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="danger">本月支出：{{ total_expenditure_month }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="danger">本周支出：{{ total_expenditure_week }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="success">本月收入：{{ total_income_month }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="success">本周收入：{{ total_income_week }} 元</el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" size="small" icon="el-icon-refresh" @click="refreshData"
          >刷新数据</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mainList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="typeName" />
      <el-table-column label="类型" align="center" prop="moneyTypeValue">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.moneyTypeValue === '0'">收入</el-tag>
          <el-tag type="danger" v-if="scope.row.moneyTypeValue === '1'">支出</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="mainMoney" />
      <el-table-column label="说明" align="center" prop="remark">
        <template slot-scope="scope">
          <div v-html="scope.row.remark"></div>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:main:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:main:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择名称">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消费金额" prop="mainMoney">
          <el-input v-model="form.mainMoney" placeholder="请输入消费金额" />
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <editor v-model="form.remark" :min-height="192" />
        </el-form-item>
        <el-form-item label="类型" prop="moneyTypeValue">
          <el-radio v-model="form.moneyTypeValue" label="0" border>收入</el-radio>
          <el-radio v-model="form.moneyTypeValue" label="1" border>支出</el-radio>
        </el-form-item>
        <el-form-item label="日期" prop="createTime">
          <el-date-picker
            v-model="form.createTime"
            type="datetime"
            placeholder="选择日期时间"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMain,
  getMain,
  delMain,
  addMain,
  updateMain,
  getAllMoneyByType,
} from "@/api/system/main";
import { listAllType } from "@/api/system/type";
export default {
  name: "Main",
  data() {
    return {
      // 总计支出
      total_expenditure: 0.0,
      // 总计收入
      total_income: 0.0,
      // 本月支出
      total_expenditure_month: 0.0,
      // 本周支出
      total_expenditure_week: 0.0,
      // 本月收入
      total_income_month: 0.0,
      // 本周收入
      total_income_week: 0.0,
      // 类型列表
      typeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务列表表格数据
      mainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        mainMoney: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [{ required: true, message: "类型编号不能为空", trigger: "blur" }],
        mainMoney: [{ required: true, message: "消费金额不能为空", trigger: "blur" }],
        createTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getAllMoneyByTypeTemp(0);
    this.getAllMoneyByTypeTemp(1);
    this.getList();
    this.getTypeList();
  },
  methods: {
    /**
     * 获取总金额-收入
     */
    getTotalIncomeMoney(type) {
      getAllMoneyByType(type).then((response) => {
        if (response.data != null) {
          this.total_income = response.data.totalIncome;
          this.total_income_month = response.data.totalIncomeMonth;
          this.total_income_week = response.data.totalIncomeWeek;
        } else {
          this.total_income = 0.0;
        }
      });
    },
    /**
     * 获取总金额-支出
     */
    getTotalExpenditureMoney(type) {
      getAllMoneyByType(type).then((response) => {
        if (response.data != null) {
          this.total_expenditure = response.data.totalExpenditure;
          this.total_expenditure_month = response.data.totalExpenditureMonth;
          this.total_expenditure_week = response.data.totalExpenditureWeek;
        } else {
          this.total_expenditure = 0.0;
        }
      });
    },
    /**
     * 获取总金额-根据类型赋值
     */
    getAllMoneyByTypeTemp(type) {
      if (type == 0) {
        this.getTotalIncomeMoney(type);
      } else {
        this.getTotalExpenditureMoney(type);
      }
    },
    /**
     * 刷新金额数据
     */
    refreshData() {
      this.getAllMoneyByTypeTemp(0);
      this.getAllMoneyByTypeTemp(1);
    },
    /**
     * 获取类型列表
     */
    getTypeList() {
      listAllType(this.queryParams).then((response) => {
        this.typeList = response.data;
      });
    },
    /** 查询财务列表列表 */
    getList() {
      this.loading = true;
      listMain(this.queryParams).then((response) => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        typeId: undefined,
        moneyTypeValue: "1",
        mainMoney: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加财务列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMain(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改财务列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMain(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除财务列表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMain(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/main/export",
        {
          ...this.queryParams,
        },
        `main_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
